package net.bsayiner.foreignExchange.Utilities;

import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class CurrencyReaderFromTcmb {

	private String currencyWebAdress = "http://www.dovizxml.com/tcmb-kurlari.html";
	private ArrayList<Currency> currencies;

	public CurrencyReaderFromTcmb() {
		currencies = new ArrayList<Currency>(12);
		currencies.add(new Currency());
	}

	public ArrayList<Currency> getCurrenciesFromWeb() {
		try {
			Document document = Jsoup.connect(currencyWebAdress).get();
			Elements info = document.select("span.kuralsatbaslik");
			for (int i = 9; i < info.size(); i += 8) {
				Currency currency = new Currency();
				currency.setCurrencyName(Jsoup.parse(info.get(i).toString()).text());
				if (i == 65) {
					currency.setBuying(Float.parseFloat(Jsoup.parse(info.get(i + 2).toString()).text()) / 100);
					currency.setSelling(Float.parseFloat(Jsoup.parse(info.get(i + 4).toString()).text()) / 100);
				} else {
					currency.setBuying(Float.parseFloat(Jsoup.parse(info.get(i + 2).toString()).text()));
					currency.setSelling(Float.parseFloat(Jsoup.parse(info.get(i + 4).toString()).text()));
				}

				currencies.add(currency);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return currencies;
	}

}
